package com.example.springboot_demo.controller;

import com.example.springboot_demo.searcher.DocSearcher;
import com.example.springboot_demo.searcher.Result;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.SpringBootVersion;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class DocSeacherController {
    //搜索
    private   static DocSearcher searcher=new DocSearcher();
    //序列化操作,把他变成json
    private ObjectMapper objectMapper = new ObjectMapper();
    @RequestMapping(value = "searcher",produces = "application/json;charset=utf-8")
  public String search(@RequestParam("query") String query) throws JsonProcessingException {
    //参数是查询词,返回值是响应内容
        //参数query来自于请求URL的query string中的query这个key的值
        ArrayList<Result> results = searcher.search(query);
        //把result 转成json的
         return objectMapper.writeValueAsString(results);
    }
}
